package com.changhong.data.service;

import com.alibaba.simpleimage.ImageFormat;
import com.alibaba.simpleimage.ImageWrapper;
import com.alibaba.simpleimage.SimpleImageException;
import com.alibaba.simpleimage.render.ScaleParameter;
import com.alibaba.simpleimage.render.WriteParameter;
import com.alibaba.simpleimage.util.ImageReadHelper;
import com.alibaba.simpleimage.util.ImageScaleHelper;
import com.alibaba.simpleimage.util.ImageWriteHelper;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;

import javax.media.jai.PlanarImage;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by lenovo on 2017/4/12.
 */
@Service
public class ImageProcessingService {
    private static final int default_width=466;
    private static final int default_height=320;
    protected static ImageFormat outputFormat = ImageFormat.JPEG;
    /**
     * 根据文件名检测是否为图像文件
     * @param imageName
     * @return
     */
    public static  boolean isPicture(String imageName) {
        if (imageName != null) {
            String tmpname = imageName.substring(imageName.lastIndexOf(".")+1 , imageName.length());
            String[] flags = {"jpg", "jpeg", "bmp", "png", "tiff", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd", "cdr", "pcd", "dxf"
                    , "ufo", "eps", "ai", "raw", "wmf"};
            for (int i = 0; i < flags.length; i++) {
                if (tmpname.toLowerCase().equals(flags[i])) {
                    return true;
                }
            }
        }
        return false;
    }

     /*
     为网站的美化缩略图一般大小为466*320pixels
     */

    /**
     *
     * @param inputStream
     * @param outputStream
     * @param width
     * @param height
     * @return
     */
     public OutputStream scale(InputStream inputStream, OutputStream outputStream, int width, int height) {

         //File out = new File(target); // 目的图片
         //FileOutputStream outStream = null;
         //File in = new File(src); // 原图片
         InputStream inStream = null;
         try {
             inStream = inputStream;
             ImageWrapper imageWrapper = ImageReadHelper.read(inStream);

             int w = imageWrapper.getWidth();
             System.out.println("原始图像的宽度"+w);
             int h = imageWrapper.getHeight();
             System.out.println("原始图像的高度"+h);
             float w1= 0f, h1 = 0f;
             float sp = (float) w / h;
             float rp = (float) width / height;
             if (sp > rp) {
                 w1 = (width * h) / (float)w;
                 h1 = width;

             } else if(sp<rp){
                 h1 = (height * w) /(float) h;
                 w1 = width;
             }else{
                 w1=width;
                 h1=height;
             }
             // 1.缩放
             ScaleParameter scaleParam = new ScaleParameter((int)w1, (int)h1, ScaleParameter.Algorithm.LANCZOS);
             PlanarImage planrImage = ImageScaleHelper.scale(imageWrapper.getAsPlanarImage(), scaleParam);
             imageWrapper = new ImageWrapper(planrImage);
             /*WriteRender writeRender=new WriteRender(imageWrapper,outputStream,outputFormat,new WriteParameter());
             writeRender.dispose();*/


             // 4.本地输出
             //outStream = new FileOutputStream(out);
             //String prefix = out.getName().substring(out.getName().lastIndexOf(".") + 1);
             ImageWriteHelper.write(imageWrapper, outputStream, outputFormat.getImageFormat("jpg"), new WriteParameter());
             return outputStream;
         } catch (SimpleImageException e) {
             e.printStackTrace();
         } finally {
             IOUtils.closeQuietly(inStream);
         }
         return outputStream;
     }
}
